package cn.edu.xmu.oomall.customer.controller;


import cn.edu.xmu.oomall.core.model.VoObject;
import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.model.vo.SimpleCartItemVo;
import cn.edu.xmu.oomall.customer.service.CartService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Api(value = "购物车", tags = "cart")
@RestController
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
/**
 * @author Haotian Cheng
 * @date 2021-12-14 12:45:27
 */
public class CartController {

    @Autowired
    private CartService cartService;


    @ApiOperation(value = "买家获得购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization ", value = "token", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数目", required = true, dataType = "integer", paramType = "query"),
    })
    @GetMapping(value = "/carts")
    @Audit
    public Object getCarts(@LoginUser Long customerId,
                           @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        ReturnObject ret;
        ReturnObject<PageInfo<VoObject>> returnObject = cartService.getCarts(customerId, page, pageSize);
        ret = Common.getPageRetObject(returnObject);
        return Common.decorateReturnObject(ret);
    }


    @ApiOperation(value = "买家将商品加入购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization ", value = "token", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "body ", value = "修改购物车单个信息", required = true, dataType = "SimpleCartItemVo", paramType = "body")
    })
    @PostMapping(value = "/carts")
    @Audit
    public Object postCarts(@LoginUser Long customerId,
                            @LoginName String customerName,
                            @Validated @RequestBody SimpleCartItemVo simpleCartItemVo) {
        return Common.decorateReturnObject(cartService.postCarts(customerId, customerName, simpleCartItemVo));
    }


    @ApiOperation(value = "买家清空购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization ", value = "token", required = true, dataType = "String", paramType = "header")
    })
    @DeleteMapping(value = "/carts")
    @Audit
    public Object deleteCarts(@LoginUser Long customerId) {
        return Common.decorateReturnObject(cartService.deleteCarts(customerId));
    }


    @ApiOperation(value = "买家修改购物车单个商品的数量和规格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization ", value = "token", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "id ", value = "购物车id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "body ", value = "修改购物车单个信息", required = true, dataType = "SimpleCartItemVo", paramType = "body")
    })
    @PutMapping(value = "/carts/{id}")
    @Audit
    public Object updateProductInCartsById(@LoginUser Long customerId, @LoginName String customerName, @PathVariable Long id, @Validated @RequestBody SimpleCartItemVo simpleCartItemVo) {
        return Common.decorateReturnObject(cartService.updateProductInCartsById(customerId, customerName, id, simpleCartItemVo));
    }


    @ApiOperation(value = "买家删除购物车中商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization ", value = "token", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "id ", value = "购物车id", required = true, dataType = "Long", paramType = "path")
    })
    @DeleteMapping(value = "/carts/{id}")
    @Audit
    public Object deleteProductInCartsById(@LoginUser Long customerId, @PathVariable Long id) {
        return Common.decorateReturnObject(cartService.deleteProductInCartsById(customerId, id));
    }


}
